package com.sptus.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sptus.common.constant.CommonConstant;
import com.sptus.common.core.domain.model.LoginUser;
import com.sptus.common.core.redis.RedisCache;
import com.sptus.system.domain.alarm.Fault;
import com.sptus.system.utils.GmailSendVerificationCode;
import com.sptus.common.utils.SecurityUtils;
import com.sptus.system.domain.*;
import com.sptus.system.domain.alarm.Alarm;
import com.sptus.system.domain.vo.AppInfo;
import com.sptus.system.domain.vo.WorkFlowVO;
import com.sptus.system.mapper.master.SptDevLogMapper;
import com.sptus.system.service.IConfigInfoService;
import com.sptus.system.service.ISptDevLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class SptDevLogServiceImpl extends ServiceImpl<SptDevLogMapper, SptDevLog> implements ISptDevLogService {


    @Autowired
    SptDevLogMapper sptDevLogMapper;
    public  static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    @Override
    public void saveWorkerLog(WorkOrders workOrders, Integer type) {
        SptDevLog devLog = new SptDevLog();
        String logInfo = "Updated tickets";
        if (type ==1 ){
            logInfo = "Added tickets";
        } else if (type == 2) {
            logInfo = "Updated tickets";
        } else if (type == 3) {
            logInfo = "The ticket was deleted";
        }
        LoginUser user= (LoginUser) SecurityUtils.getLoginUser();
        devLog.setUserId(user.getUserId() + "");
        devLog.setUserName(user.getUsername());
        devLog.setUserType(2);
        devLog.setLogType("workOrders");
        devLog.setLogInfo(logInfo);
        devLog.setDeviceId(workOrders.getDeviceSn());
        save(devLog);
    }

    @Override
    public void saveDevSet(DevDictionaries devDictionaries, String type) {
        SptDevLog devLog = new SptDevLog();
        String logInfo = "Set: "+devDictionaries.getName() +" The parameters are "+devDictionaries.getValue();
        LoginUser user= (LoginUser) SecurityUtils.getLoginUser();
        devLog.setUserId(user.getUserId() + "");
        devLog.setUserName(user.getUsername());
        devLog.setUserType(2);
        devLog.setLogType("Device Set");
        devLog.setLogInfo(logInfo);
        devLog.setDeviceId(devDictionaries.getSN());
        save(devLog);
    }

    @Override
    public IPage<SptDevLog> getSptLogs(Page page, QueryWrapper queryWrapper) {
        return sptDevLogMapper.getSptLogs(page, queryWrapper);
    }

    @Override
    public void saveDevSetUpdate(DevDictionaries devDictionaries, String number) {
        SptDevLog devLog = new SptDevLog();
        String logInfo ="";
        if(number.equals("0")){
             logInfo = "Dsp Update: "+devDictionaries.getName()+" The parameters are "+devDictionaries.getValue();
            devLog.setLogType("DSP Update");
        }else if("9".equals(number)){
            logInfo="Remote Update : "+devDictionaries.getName()+" The parameters are "+devDictionaries.getValue();
            devLog.setLogType("Remote Update");
        }
        LoginUser user= (LoginUser) SecurityUtils.getLoginUser();
        devLog.setUserId(user.getUserId() + "");
        devLog.setUserName(user.getUsername());
        devLog.setUserType(2);
        devLog.setLogInfo(logInfo);
        devLog.setDeviceId(devDictionaries.getSN());
        save(devLog);
    }

    @Override
    public void saveDevSetApp(AppInfo appInfo,String version) {
        SptDevLog devLog = new SptDevLog();
        if(!appInfo.getCode().equals("OTA")){
            String logInfo = "Set: "+appInfo.getCode() +" The parameters are "+appInfo.getValue();
            devLog.setUserId(appInfo.getUserId() + "");
            devLog.setUserName(appInfo.getEmail());
            devLog.setUserType(1);
            devLog.setLogType("Device Set");
            devLog.setLogInfo(logInfo);
            devLog.setDeviceId(appInfo.getSn());

        }else {
            String logInfo ="";

            if(appInfo.getValue().equals("0")){
                logInfo = "Dsp Update: "+appInfo.getCode() +" The parameters are "+version;
                devLog.setLogType("DSP Update");
            }else if(appInfo.getValue().equals("9")){
                logInfo="Remote Update : "+appInfo.getCode() +" The parameters are "+version;
                devLog.setLogType("Remote Update");
            }
            devLog.setUserId(appInfo.getUserId());
            devLog.setUserName(appInfo.getEmail());
            devLog.setUserType(2);
            devLog.setLogInfo(logInfo);
            devLog.setDeviceId(appInfo.getSn());

        }
        save(devLog);

    }
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    @Override
    public void saveDevAlarm(String sn, BBWS bbws,WorkOrdersServiceImpl workOrdersService ,RedisCache redisCache) {
        if (bbws != null) {
            ArrayList<SptDevLog>  devLogs = new ArrayList<>();
            if(!bbws.getOverallOvervoltageAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Overall Overvoltage Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);

                if(redisCache.getCacheObject("spt:"+sn+":alarm:OverallOvervoltageAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallOvervoltageAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:OverallOvervoltageAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCategory("Alarm");
                        workOrders.setCustomerId("1");
                        workOrders.setTesolution("Alarm:"+sn+": Overall Overvoltage Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallOvervoltageAlarm",(i+1), 2,TimeUnit.HOURS);
                }

            }
            if(!bbws.getOverallUnderovervoltageAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Overall UnderOvervoltage Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:OverallUnderOvervoltageAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallUnderOvervoltageAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:OverallUnderOvervoltageAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm:"+sn+": Overall UnderOvervoltage Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallUnderOvervoltageAlarm",(i+1),2, TimeUnit.HOURS);
                }
            }
            if(!bbws.getCellUnderovervoltageAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Cell UnderOvervoltage Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:CellUnderOvervoltageAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:CellUnderOvervoltageAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:CellUnderOvervoltageAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm:"+sn+": Cell UnderOvervoltage Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:CellUnderOvervoltageAlarm",(i+1),2, TimeUnit.HOURS);
                }
            }
            if(!bbws.getCellOvervoltageAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Cell Overvoltage Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:CellOvervoltageAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:CellOvervoltageAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:CellOvervoltageAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm:"+sn+": Cell Overvoltage Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:CellOvervoltageAlarm",(i+1),2, TimeUnit.HOURS);
                }
            }
            if(!bbws.getChargingCurrentAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Charging Current Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:ChargingCurrentAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:ChargingCurrentAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:ChargingCurrentAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCategory("Alarm");
                        workOrders.setCustomerId("1");
                        workOrders.setTesolution("Alarm:"+sn+": Charging Current Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:ChargingCurrentAlarm",(i+1),2, TimeUnit.HOURS);
                }

            }
            if(!bbws.getDisChargingCurrentAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Discharging Current Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:DischargingCurrentAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:DischargingCurrentAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:DischargingCurrentAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm: Discharging Current Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:DischargingCurrentAlarm",(i+1),2, TimeUnit.HOURS);
                }

            }
            if(!bbws.getOverallChargingTemperatureAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Overall Charging Temperature Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:OverallChargingTemperatureAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallChargingTemperatureAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:OverallChargingTemperatureAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm:"+sn+": Overall Charging Temperature Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallChargingTemperatureAlarm",(i+1),2, TimeUnit.HOURS);
                }


            }
            if(!bbws.getOverallDischargingTemperatureAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Overall Discharging Temperature Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:OverallDischargingTemperatureAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallDischargingTemperatureAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:OverallDischargingTemperatureAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCustomerId("1");
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm: Overall Discharging Temperature Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:OverallDischargingTemperatureAlarm",(i+1),2, TimeUnit.HOURS);
                }

            }
            if(!bbws.getBatteryChargingTemperatureAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Battery Charging Temperature Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:BatteryChargingTemperatureAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:BatteryChargingTemperatureAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:BatteryChargingTemperatureAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setPriority(3);
                        workOrders.setCategory("Alarm");
                        workOrders.setCustomerId("1");
                        workOrders.setTesolution("Alarm:"+sn+": Battery Charging Temperature Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:BatteryChargingTemperatureAlarm",(i+1),2, TimeUnit.HOURS);
                }
            }
            if(!bbws.getBatteryDisChargingTemperatureAlarmState().equals("0")){
                SptDevLog devLog = new SptDevLog();
                String logInfo = "Alarm: Battery Discharging Temperature Alarm";
                devLog.setLogType("alarm");
                devLog.setLogInfo(logInfo);
                devLog.setDeviceId(sn);
                devLogs.add(devLog);
                if(redisCache.getCacheObject("spt:"+sn+":alarm:BatteryDischargingTemperatureAlarm")==null){
                    redisCache.setCacheObject("spt:"+sn+":alarm:BatteryDischargingTemperatureAlarm",1,2, TimeUnit.HOURS);
                }else {
                    Integer i=redisCache.getCacheObject("spt:"+sn+":alarm:BatteryDischargingTemperatureAlarm");
                    if(CommonConstant.ALARM_NUM==i){
                        WorkOrders workOrders =new WorkOrders();
                        workOrders.setDeviceSn(sn);
                        workOrders.setStatus("To Do");
                        workOrders.setCustomerId("1");
                        workOrders.setPriority(3);
                        workOrders.setCategory("Alarm");
                        workOrders.setTesolution("Alarm:"+sn+": Battery Discharging Temperature Alarm");
                        List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                        workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                        workOrdersService.save(workOrders);
                    }
                    redisCache.setCacheObject("spt:"+sn+":alarm:BatteryDischargingTemperatureAlarm",(i+1),2, TimeUnit.HOURS);
                }
            }
            this.saveBatch(devLogs);

        }
    }


    @Async("asyncExecutor")
    public void saveDevAlarmW(Equipment equipment, List<Alarm> alarms, WorkOrdersServiceImpl service, RedisCache redisCache, IConfigInfoService configInfoService) {
        ArrayList<SptDevLog> devLogs = new ArrayList<>();
        for (Alarm alarm : alarms) {
            SptDevLog devLog = new SptDevLog();
            String logInfo = "Alarm:" + alarm.getDescription();
            devLog.setLogType("alarm");
            devLog.setLogInfo(logInfo);
            devLog.setDeviceId(equipment.getSn());
            devLog.setStatus(alarm.getGrade());
            devLogs.add(devLog);
        }
        // 添加日志
        this.saveBatch(devLogs);

        List<Alarm> highPriorityAlarms = alarms.stream()
                .filter(e -> e.getGrade() == 2 || e.getGrade() == 3)
                .collect(Collectors.toList());

        // 批量工单收集
        List<WorkOrders> workOrdersList = new ArrayList<>();

        if (!highPriorityAlarms.isEmpty()) {
            for (Alarm alarm : highPriorityAlarms) {
                boolean shouldSaveWorkOrder = redisCache.judgeAlarmTimesAndRenewal(
                        equipment.getSn(),
                        alarm.getCode(),
                        alarm.getAlarmCount(),
                        alarm.getExpireTime()
                );
                if (shouldSaveWorkOrder) {
                    String description = alarm.getDescription() + ";";
                    WorkOrders workOrders = new WorkOrders();
                    workOrders.setDeviceSn(equipment.getSn());
                    workOrders.setStatus("To Do");
                    workOrders.setCustomerId("1");
                    workOrders.setPriority(3);
                    workOrders.setCategory("Alarm");
                    workOrders.setTesolution("Alarm:" + equipment.getSn() + ": " + description);
                    List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
                    workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
                    workOrdersList.add(workOrders);
                }
            }
            // 去重：只保存数据库中不存在的工单
            if (!workOrdersList.isEmpty()) {
                service.saveBatch(workOrdersList);
            }
        }
    }

    @Async("asyncExecutor")
    @Override
    public void saveDevFault(Equipment equipment, Fault fault, WorkOrdersServiceImpl workOrdersService, RedisCache redisCache, IConfigInfoService configInfoService) {
        Integer i = redisCache.getCacheObject("spt:" + equipment.getSn() + ":alarm:DevFault")==null?0:redisCache.getCacheObject("spt:" + equipment.getSn() + ":alarm:DevAlarm");
        if (i ==0) {
            redisCache.setCacheObject("spt:" + equipment.getSn() + ":alarm:DevFault", 1, 2, TimeUnit.HOURS);
            //添加工单
            WorkOrders workOrders =new WorkOrders();
            workOrders.setDeviceSn(equipment.getSn());
            workOrders.setStatus("To Do");
            workOrders.setCustomerId("1");
            workOrders.setPriority(3);
            workOrders.setCategory("Alarm");
            workOrders.setTesolution("Alarm:"+equipment.getSn()+": "+fault.getEnglish());
            List<WorkFlowVO> workFlowVOS = WorkFlowVO.newListWorkFlowVO("IOT:service", "IOT");
            workOrders.setDescription(JSONObject.toJSONString(workFlowVOS));
            workOrdersService.save(workOrders);
        }else {
            redisCache.setCacheObject("spt:" + equipment.getSn() + ":alarm:DevFault", (i + 1), 2, TimeUnit.HOURS);
        }
        SptDevLog devLog = new SptDevLog();
        String logInfo = "fault:" + fault.getEnglish();
        devLog.setLogType("fault");
        devLog.setLogInfo(logInfo);
        devLog.setDeviceId(equipment.getSn());
        devLog.setStatus(3);
        //添加日志
        this.save(devLog);

            List<ConfigInfo> configInfos = configInfoService.getBaseMapper().selectList(null);
            Map<String, String> DataLoaderConfig = configInfos.stream().collect(Collectors.toMap(ConfigInfo::getName, ConfigInfo::getValue));
            if (i % CommonConstant.ALARM_NUM == 0) {
                //发送邮箱
                String from = DataLoaderConfig.get("spt.auth.code.email"); // 发件人邮箱（你的Gmail）
                String host = DataLoaderConfig.get("spt.auth.code.email.host"); // SMTP服务器地址
                String port = DataLoaderConfig.get("spt.auth.code.email.post"); // Gmail SMTP端口
                String username = DataLoaderConfig.get("spt.auth.code.email.username");
                String password = DataLoaderConfig.get("spt.auth.code.email.pass");
                String reviewers = DataLoaderConfig.get("spt.alarm.email");
                try {
                    cachedThreadPool.submit(new Thread(new Runnable() {
                        @Override
                        public void run() {
                            GmailSendVerificationCode.sendEmailWithFault(reviewers, equipment, fault, from, host, port, username, password);
                        }
                    }));
                } catch (Exception e) {
                    //自定义异常
                    e.printStackTrace();
                    throw new RuntimeException("邮箱发送失败");
                }

            }


    }


}
